import constants from "share/constants";
import errorMessage from "share/errorMessage";
import { messages } from "share/common";
import { Modal, message } from "antd";
import moment from "moment";

export default {
  //获取单据状态
  getReportStatus: (info) => {
    const status = constants.documentStatus;
    let result = {};
    info &&
      status.map((item) => {
        if (
          item.value === String(info.status) ||
          item.value === String(info.status * 10000 + info.rejectType)
        ) {
          result = item;
        }
      });
    return result;
  },

  //处理费用列表数据
  filterExpenseList: (list) => {
    const showList = [];
    list.map((item) => {
      item.invoiceView.status = item.status;
      // https://pmo.mcd.com.cn/jira/browse/TES-786
      item.invoiceView.auditReply = item.auditReply;
      item.invoiceView.approvalStatus = item.approvalStatus;
      item.invoiceView.auditReplyCode = item.auditReplyCode;
      item.invoiceView.remark = item.remark;
      item.invoiceView.rejectReason = item.rejectReason;
      item.invoiceView.apportionmentAmount = item.apportionmentAmount;
      item.invoiceView.washAmount = item.washAmount;
      showList.push(item.invoiceView);
    });
    return showList;
  },

  //报销单详情底部是否显示审批操作按钮
  isApproveButtonsShow: (approvalChains, currentUserId, status) => {
    let showBottom = false;
    if (status !== 1002) {
      //1002:审批中
      return false;
    }
    if (approvalChains[0]) {
      if (approvalChains[0].approverOID === currentUserId) {
        showBottom = true;
      }
    }
    if (approvalChains) {
      approvalChains.forEach((approver) => {
        if (approver.approverOID === currentUserId) {
          showBottom = true;
        } else if (approver.proxyApproverOIDs) {
          approver.proxyApproverOIDs.forEach((proxyApprover) => {
            if (proxyApprover === currentUserId) {
              showBottom = true;
            }
          });
        }
      });
    }
    return showBottom;
  },

  //处理报销单提交时的报错信息
  handleSubmitError: (e) => {
    if (!e.response) {
      console.error('handleSubmitError got unexpected error:', e)
      message.error(e.message);
      return
    }
    const data = e.response.data || {};
    if (data.errorCode === "6017007") {
      Modal.warning({
        title: messages(
          "expense-parent-report.key166"
        ) /*您的免费试用已经到期，请联系汇联易*/,
        content: "400 829 7878",
      });
      return;
    }
    if (data.errorCode === "2001") {
      message.error(messages("expense-parent-report.key14" /*审批链为空*/));
    } else if (data.errorCode === "13001") {
      message.error(data.message);
    } else {
      errorMessage(e.response);
    }
  },

  //渲染Modal.info
  renderModalInfo: (type) => {
    switch (type) {
      //外部费用导入
      case "externalExpenseImport":
        return Modal.info({
          title: messages(
            "expense-parent-report.key20" /*导入过程预计需要15分钟*/
          ),
          content: (
            <div>
              <p>
                {messages(
                  "expense-parent-report.key21" /*在此期间请不要修改报销单*/
                )}
              </p>
              <p>
                {messages(
                  "expense-parent-report.key22" /*导入完成后,您会收到一封邮件通知,然后刷新报销单页面进行查看*/
                )}
              </p>
            </div>
          ),
        });
      default:
        return false;
    }
  },

  //费用列排序比较
  invoiceColumnsCompare: (columnKey, order) => {
    return function (obj1, obj2) {
      if (!columnKey) return 0;
      if (columnKey === "expenseTypeName") {
        let b = obj2[columnKey];
        let a = obj1[columnKey];
        let temp = a.localeCompare(b, "zh-Hans-CN", { sensitivity: "accent" });
        if (temp === -1) {
          return order === "descend" ? 1 : -1;
        }
        if (temp === 1) {
          return order === "descend" ? -1 : 1;
        }
        if (order === "descend") {
          return 0;
        }
        return 0;
      }
      let val1 = obj1[columnKey];
      let val2 = obj2[columnKey];
      if (!isNaN(Number(val1)) && !isNaN(Number(val2))) {
        val1 = Number(val1);
        val2 = Number(val2);
      }
      if (val1 < val2) {
        return order === "descend" ? 1 : -1;
      }
      if (val1 > val2) {
        return order === "descend" ? -1 : 1;
      }
      return 0;
    };
  },

  // 获取时差
  // timeZoneOffset：单据保存时区 与 0时区 的时差
  // nowTimeZoneOffset：当前时区 与 0时区 的时差
  // timeOffset：单据保存时区 与 当前时区 的时差
  getTimeOffset(timeZoneOffset) {
    let timeOffset = 0;
    const nowTimeZoneOffset = -new Date().getTimezoneOffset();
    if (
      (timeZoneOffset || timeZoneOffset === 0) &&
      timeZoneOffset !== nowTimeZoneOffset
    ) {
      timeOffset = timeZoneOffset - nowTimeZoneOffset;
    }
    return timeOffset;
  },
  // 检查报销单内费用标签是否存在必填未输
  expenseVoiceLabelValidator() {
    const { expenseReportInvoices } = this.state;
    let error = [];
    expenseReportInvoices.forEach((item) => {
      item.invoiceView.invoiceLabels.some((label) => {
        if (label.type === "NO_VALUE") {
          error.push({
            name: item.invoiceView.expenseTypeName,
            createDate: item.invoiceView.createdDate,
          });
          return true;
        }
      });
    });
    error = error.map((item) => (
      <p>
        {moment(new Date(item.createDate)).format("YYYY-MM-DD")}&nbsp;
        {item.name}&nbsp; {messages("expense-1.key443") /*存在必填字段为空*/}
      </p>
    ));
    error.unshift(
      <p>
        {
          messages("expense-1.key444", {
            arg1: error.length,
          }) /*请补全必填未输的费用，已自动搜索出{arg1}笔*/
        }
      </p>
    );
    if (!expenseReportInvoices[0]) {
      message.error(messages("expense-1.key445") /*请先添加费用*/);
      return false;
    }
    if (error[1]) {
      Modal.warning({
        title: messages("expense-1.key446") /*提交结果*/,
        content: error,
        okText: messages("expense-1.key48") /*返回修改*/,
      });
      return false;
    }
    return true;
  },
  corporatePaymentReportValidator(expenseReportInvoices, payInfoList) {
    let error = [];
    if (!payInfoList.length) {
      message.error(messages("expense-1.key729") /*请先添加付款行*/);
      return false;
    }
    expenseReportInvoices.forEach((item) => {
      item.invoiceView.invoiceLabels.some((label) => {
        if (label.type === "NO_VALUE") {
          error.push({
            name: item.invoiceView.expenseTypeName,
            createDate: item.invoiceView.createdDate,
          });
          return true;
        }
      });
    });
    error = error.map((item) => (
      <p>
        {moment(new Date(item.createDate)).format("YYYY-MM-DD")}&nbsp;
        {item.name}&nbsp; {messages("expense-1.key443") /*存在必填字段为空*/}
      </p>
    ));
    error.unshift(
      <p>
        {
          messages("expense-1.key444", {
            arg1: error.length,
          }) /*请补全必填未输的费用，已自动搜索出{arg1}笔*/
        }
      </p>
    );
    if (error[1]) {
      Modal.warning({
        title: messages("expense-1.key446") /*提交结果*/,
        content: error,
        okText: messages("expense-1.key48") /*返回修改*/,
      });
      return false;
    }
    return true;
  },
};
